package org.csu.mypetstore.domin;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.*;

@Data
@TableName("banner")
public class Banner {
    String username;
    @TableField(value = "BIRDS")
    int BIRDS;
    @TableField(value = "CATS")
    int CATS;
    @TableField(value = "DOGS")
    int DOGS;
    @TableField(value = "FISH")
    int FISH;
    @TableField(value = "REPTILES")
    int REPTILES;

    public List<String> sort() throws IllegalAccessException {
        Map<String,Integer> map = new HashMap<>();
        Field[] fields = this.getClass().getDeclaredFields();
        //将(属性，值)放入map
        for(Field field : fields){
            //设置允许访问私有变量
            field.setAccessible(true);
            String name = field.getName();
            if(!name.equals("username")){
                int value = Integer.parseInt(field.get(this).toString());
                map.put(name,value);
            }
        }
        //对值进行降序排序
        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet()); //转换为list
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
        List<String> result = new ArrayList<>();
        for (Map.Entry<String, Integer> mapping : list) {
            result.add(mapping.getKey());
        }
        return result;
    }
    public Banner(String username){
        this.username = username;
    }
}
